package com.jy.project.service.imp;

import com.github.pagehelper.PageHelper;
import com.jy.project.domain.Apply;
import com.jy.project.domain.Sign;
import com.jy.project.domain.ToDoTasks;
import com.jy.project.domain.User;
import com.jy.project.dto.SignDTO;
import com.jy.project.dto.SignQueryDTO;
import com.jy.project.dto.ToDoTasksDTO;
import com.jy.project.ex.BizException;
import com.jy.project.mapper.ApplyMapper;
import com.jy.project.mapper.SignMapper;
import com.jy.project.mapper.ToDoTasksMapper;
import com.jy.project.mapper.UserMapper;
import com.jy.project.service.ToDoTasksService;
import com.jy.project.vo.SignVO;
import com.jy.project.vo.ToDoTasksVO;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class ToDoTasksServiceImpl implements ToDoTasksService {

    @Autowired
    private ToDoTasksMapper toDoTasksMapper;

    @Autowired
    private SignMapper signMapper;


    @Autowired
    private ApplyMapper applyMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<ToDoTasksVO> listToDoTasksVO(ToDoTasksDTO dto) {
        SignDTO signDTO=new SignDTO();
        signDTO.setApplyNo(dto.getApplyNo());
        signDTO.setChildUserId(dto.getUserId());
        if (dto.getStatus()==0) {
            List<Integer> status = new ArrayList<>();
            status.add(dto.getStatus());
            signDTO.setStatusList(status);
        }
        if (dto.getStatus()==1) {
            List<Integer> status = new ArrayList<>();
            status.add(1);
            status.add(2);
            signDTO.setStatusList(status);
        }
        if (dto.getStatus()==3) {
            dto.setIsEnd(0);
        }
        List<Sign> list = signMapper.listSign(signDTO);
        if (list.isEmpty()){
            return  new ArrayList<>();
        }
        List<String> applyNos=new ArrayList<>();
        for (Sign s : list) {
            applyNos.add(s.getApplyNo());
        }
        dto.setListApplyNo(applyNos);
        PageHelper.startPage(dto);
        List<ToDoTasksVO> tasksVOList = toDoTasksMapper.listToDoTasksVO(dto);
        return tasksVOList;
    }

    @Override
    public ToDoTasksVO getToDoTasksVO(ToDoTasksDTO dto) {
        Example example=new Example(ToDoTasks.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete",0)
                 .andEqualTo("applyNo",dto.getApplyNo());
        ToDoTasks tasks = toDoTasksMapper.selectOneByExample(example);
        ToDoTasksVO vo=new ToDoTasksVO();
        BeanUtils.copyProperties(tasks,vo);
        return vo;
    }

    @Override
    public List<SignVO> listSignVO(SignQueryDTO dto) {
        Sign signParam=new Sign();
        signParam.setIsDelete(0);
        signParam.setApplyNo(dto.getApplyNo());
        List<Sign> list = signMapper.select(signParam);
        List<SignVO> signVOS=new ArrayList<>();
        for (Sign sign : list) {
            SignVO vo=new SignVO();
            BeanUtils.copyProperties(sign,vo);
            signVOS.add(vo);
        }
        return signVOS;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertSign(SignDTO dto) {
        Example example=new Example(ToDoTasks.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete",0)
                .andEqualTo("applyNo",dto.getApplyNo());
        ToDoTasks tasks = toDoTasksMapper.selectOneByExample(example);
        if (tasks==null){
            throw  new BizException("当前任务不存在");
        }
        if (tasks.getIsEnd()==1){
            throw  new BizException("当前任务已经结束");
        }
        Long userId = dto.getUserId();
        String applyNo = dto.getApplyNo();
        /*Sign s = new Sign();
        s.setUserId(userId);
        s.setApplyNo(applyNo);
        List<Sign> list = signMapper.select(s);
        if (!list.isEmpty()){
            throw  new BizException("已经操作过加签");
        }*/
        Integer type = dto.getType();
        Long childUserId = dto.getChildUserId();
        Sign si = new Sign();
        si.setChildUserId(childUserId);
        si.setApplyNo(applyNo);
        List<Sign> signList = signMapper.select(si);
        if (!signList.isEmpty()){
            throw  new BizException("已经在加签处理人列表中");
        }
        Sign sign=new Sign();
        sign.setStatus(0);
        sign.setUserId(userId);
        User u = userMapper.selectByPrimaryKey(userId);
        String userNickName = u.getNickName();
        sign.setUserNickName(userNickName);
        User user = userMapper.selectByPrimaryKey(childUserId);
        String nickName = user.getNickName();
        sign.setChildNicKName(nickName);
        sign.setChildUserId(childUserId);
        sign.setApplyNo(applyNo);
        Date date = new Date();
        sign.setCreateAt(date);
        sign.setUpdateDay(date);
        sign.setUpdateAt(date);
        sign.setType(type);
        sign.setRemarks(dto.getRemarks());
        sign.setApprovalOpinions(dto.getApprovalOpinions());
        sign.setIsDelete(0);
        boolean bl= signMapper.insertSelective(sign)>0;
        if (bl==false){
            throw  new BizException("加签过了失败");
        }
        return bl;
    }

    @Override
    public ToDoTasksVO getToDoTasksVO(SignQueryDTO dto) {
        Sign s=new Sign();
        s.setUserId(dto.getUserId());
        s.setApplyNo(dto.getApplyNo());
        Sign g = signMapper.selectOne(s);
        if (g==null){
            return null;
        }
        ToDoTasksVO vo=new ToDoTasksVO();
        BeanUtils.copyProperties(g,vo);
        return vo;

    }

    @Override
    public boolean operation(SignQueryDTO dto) {
        Long id = dto.getId();
        Sign s=new Sign();
        s.setId(id);
        s.setStatus(dto.getStatus());
        return signMapper.updateByPrimaryKeySelective(s)>0;
    }

    @Override
    public boolean isAllAgree(SignQueryDTO dto) {
        Sign signParam=new Sign();
        signParam.setIsDelete(0);
        signParam.setApplyNo(dto.getApplyNo());
        List<Sign> list = signMapper.select(signParam);
        for (Sign sign : list) {
            if (sign.getStatus()!=1){
                return false;
            }
        }
        return true;
    }

    @Override
    public SignVO getChildSignVO(SignQueryDTO dto) {
        Long userId = dto.getUserId();
        String applyNo = dto.getApplyNo();
        Sign sign = new Sign();
        sign.setChildUserId(userId);
        sign.setApplyNo(applyNo);
        List<Sign> select = signMapper.select(sign);
        if (select.isEmpty()){
            throw new BizException("没有当前记录");
        }
        if (select.size()==1){
            SignVO signVO=new SignVO();
            Sign s = select.get(0);
            BeanUtils.copyProperties(s,signVO);
            return signVO;
        }
        if (select.size()==2){
            SignVO signVO=new SignVO();
            Sign s = select.get(0);
            if (s.getStatus()==0){
                BeanUtils.copyProperties(s,signVO);
                return signVO;
            }
            Sign s2 = select.get(1);
            BeanUtils.copyProperties(s2,signVO);
            return signVO;
        }
        if (select.size()==3){
            SignVO signVO=new SignVO();
            Sign s = select.get(0);
            if (s.getStatus()==0){
                BeanUtils.copyProperties(s,signVO);
                return signVO;
            }
            Sign s2 = select.get(1);
            if (s2.getStatus()==0){
                BeanUtils.copyProperties(s2,signVO);
                return signVO;
            }
            Sign s3 = select.get(2);
            BeanUtils.copyProperties(s3,signVO);
            return signVO;
        }
        throw new BizException("此加签流程有问题,请联系管理员");
    }

    @Override
    public boolean isChildForwardSign(SignQueryDTO dto) {
        SignVO childSignVO = getChildSignVO(dto);
        if (childSignVO==null){
            return  false;
        }
        Integer type = childSignVO.getType();
        if (type==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateToDoTasksEnd(String applyNo) {
        ToDoTasks tasks=new ToDoTasks();
        tasks.setIsEnd(1);
        Example example=new Example(ToDoTasks.class);
        example.createCriteria().andEqualTo("applyNo",applyNo);
        int i = toDoTasksMapper.updateByExampleSelective(tasks, example);
        return  i>0;
    }

    @Override
    public boolean alreadyHandle(Long id) {
        ToDoTasks t=new ToDoTasks();
        t.setId(id);
        t.setStatus(1);
        t.setIsEnd(1);
        int i = toDoTasksMapper.updateByPrimaryKeySelective(t);
        return  i>0;
    }

    @Override
    public boolean rejectHandle(Long id) {
        ToDoTasks t=new ToDoTasks();
        t.setId(id);
        t.setStatus(2);
        t.setIsEnd(1);
        int i = toDoTasksMapper.updateByPrimaryKeySelective(t);
        return  i>0;
    }

    @Override
    public List<ToDoTasks> listToDoTaskByApplyNo(String applyNo) {
        Example example=new Example(ToDoTasks.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete",0)
                .andEqualTo("applyNo",applyNo);
        return toDoTasksMapper.selectByExample(example);
    }

    @Override
    public boolean isAllAgree(String applyNo) {
        List<ToDoTasks> toDoTasks = listToDoTaskByApplyNo(applyNo);
        if (toDoTasks.isEmpty()){
            return  false;
        }
        for (ToDoTasks toDoTask : toDoTasks) {
            Integer end = toDoTask.getIsEnd();
            if (end==0){
                return  false;
            }
        }
        return true;
    }

}
